import CryptoJs from 'crypto-js' //引用AES源码js

const KEY = CryptoJs.enc.Utf8.parse("lzc@jzdmlll@1234"); //密钥  密码
const IV = CryptoJs.enc.Utf8.parse("lzc@jzdmlll@1234");
const CryptoInfo = {
  //加密
  encryptByAES: data => {
    let key = KEY;
    let iv = IV;
    let srcs = CryptoJs.enc.Utf8.parse(data);
    let encrypted = CryptoJs.AES.encrypt(srcs, key, {
      iv: iv,
      mode: CryptoJs.mode.CBC,
      padding: CryptoJs.pad.ZeroPadding
    });
    // console.log("-=-=-=-", encrypted.ciphertext)
    return CryptoJs.enc.Base64.stringify(encrypted.ciphertext) + "-" + new Date().getTime();
  },
  // 解密, 调用该方法时，传入的data是base64的密文
  decryptByAES: data => {
    let key = KEY;
    let iv = IV;
    let base64 = CryptoJs.enc.Base64.parse(data);
    let src = CryptoJs.enc.Base64.stringify(base64);

    let decrypt = CryptoJs.AES.decrypt(src, key, {
      iv: iv,
      mode: CryptoJs.mode.CBC,
      padding: CryptoJs.pad.ZeroPadding
    });

    let decryptedStr = decrypt.toString(CryptoJs.enc.Utf8);
    return decryptedStr.toString();
  }
};
export {
  CryptoInfo
}
